package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

type SysUser struct {
	BaseModel
	Username   string `orm:"column(username);description(用户名)"`
	Password   string `orm:"column(password);description(密码)"`
	RealName   string `orm:"column(real_name);description(姓名)"`
	HeadUrl    string `orm:"column(head_url);description(头像)"`
	Gender     int    `orm:"column(gender);description(用户性别，0: 未知；1:男性；2:女性)"`
	PersonCode string `orm:"column(person_code);description(人员编号)"`
	DeptId     int64  `orm:"column(dept_id);description(部门ID)"`
	SuperAdmin int    `orm:"column(super_admin);description(超级管理员 0：否 1：是)"`
}

func init() {
	orm.RegisterModel(new(SysUser))
}

// SearchField 定义模型的可搜索字段
func (*SysUser) SearchField() []string {
	return []string{"name", "real_name", "person_code"}
}

// WhereField 定义模型可作为条件的字段
func (*SysUser) WhereField() []string {
	return []string{"id", "status", "name", "real_name", "person_code"}
}

func (m *SysUser) TableName() string {
	return "sys_user"
}

func (m *SysUser) IsSuperAdmin() bool {
	return m.SuperAdmin == 1
}

// AddSysUser insert a new SysUser into database and returns
// last inserted Id on success.
func AddSysUser(m *SysUser, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetSysUserById retrieves SysUser by Id. Returns error if
// Id doesn't exist
func GetSysUserById(id int64) (v *SysUser, err error) {
	o := orm.NewOrm()
	v = &SysUser{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(SysUser)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateSysUserById updates SysUser by Id and returns error if
// the record to be updated doesn't exist
func UpdateSysUserById(m *SysUser, userId int64) (err error) {
	o := orm.NewOrm()
	v := SysUser{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteSysUser deletes SysUser by Id and returns error if
// the record to be deleted doesn't exist
func DeleteSysUser(id int64) (err error) {
	o := orm.NewOrm()
	v := SysUser{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&SysUser{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroySysUser Destroy SysUser by Id and returns error
func DestroySysUser(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := SysUser{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
